/*
* @author : TF-BJ-C064
* @creation : 2014-6-27 下午4:30:37
* @description : 
*
*/

package com.action.park;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.base.BaseAction;
import com.base.UserBean;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.bean.ParkStatBean;
import com.bean.Parkinfo;
import com.bean.PerformanceBean;
import com.dao.ParkOrderDao;
import com.service.ParkOrderService;
import com.service.ParkStatService;
import com.service.ParkinfoService;
import com.tool.DateTimeTools;
import com.tool.RegexTool;
import com.util.GUtil;

public class ParkStatAction extends BaseAction{

	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	private static String Tag = MyPayStatisticsAction.class.getName();
	ParkStatService statService;
	ParkOrder parkOrder;
	List<ParkOrder> list;
	List<ParkStatBean> listPF;
	List<Parkinfo> listPark;
	Parkinfo parkInfo;
	ParkManager parkManager;
	String datetime;
	Timestamp tt;
	String stattype; // day, week, month, year
	JSONObject json;

	Boolean queryMyOrder=true;//true: 收费员查询自己的订单 ; false查询所有订单
	Timestamp t;
	@Override
	public String handle() throws Exception {
		this.setHeaderTitle("停车场统计");
		return GUtil.SUCCESS;
	}
	
	public String months()throws Exception{
		
		listPF = statService.moneylist(datetime, page, parkOrder);
		session.put("listPF", listPF);
		session.put("datetime", datetime);
		JSONObject json = new JSONObject();
		json.put("error", 0);
		json.put("list", listPF);
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.close();
		
		return GUtil.NONE;
	}
	public String weeks()throws Exception{
			
			listPF = statService.moneylistzhou(datetime, page, parkOrder);
			session.put("listPF", listPF);
			session.put("datetime", datetime);
			JSONObject json = new JSONObject();
			json.put("error", 0);
			json.put("list", listPF);
			PrintWriter out = response.getWriter();
			out.print(json.toString());
			out.close();
			
			return GUtil.NONE;
		}
	public String queryMoney() throws Exception{
		this.setHeaderTitle("车场收入统计");
		log.info("ParkStatAction");
		boolean bPass = true;
		if(stattype==null){
			bPass = false;
			this.setMsg("类型错误"); 
		}
		if(datetime==null){
			bPass = false;
			this.setMsg("时间类型错误"); 
		}
		if(bPass){
			if(stattype.equals("day")){
				return this.months();
			}else if(stattype.equals("week")){
				return this.weeks();
			}else if(stattype.equals("month")){
				return this.months();
			}else if(stattype.equals("year")){
				return this.months();
			}
			this.setMsg("参数错误  ");
		}
		return GUtil.NONE;
	}
	/**
	 * 按天查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByDay() throws Exception{
		String daytime = datetime.substring(0,10).trim();
		log.info(Tag+".queryByDay() daytime="+daytime);
		List<Parkinfo> payInOut = new ArrayList<Parkinfo>();
		List<Parkinfo> payStat = new ArrayList<Parkinfo>();
		json = new JSONObject();

		if(RegexTool.checkDateTime(daytime)){
			String ret = statService.queryByDayo(payInOut,payStat,daytime, user.getId());//
			if(ret.endsWith(GUtil.SUCCESS)){
				json.put("error", 0);
				json.put("msg", "操作成功");
				json.put("payInOut", JSONArray.fromObject( payInOut ) );
				json.put("payStat", JSONArray.fromObject( payStat ) );
			}else{
				json.put("error", -1);
				json.put("msg", "操作失败 "+ret);
			}
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 日期格式错误");
		}
		log.info(Tag+" "+json.toString());
		
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.close();

		return GUtil.NONE;
		
	}
	/**
	 * 按周查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByWeek() throws Exception{
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkDateTime(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//String datetime = "2014-01-08";
		Date timelong = formatDate.parse(datetime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getWeekDays(inTime, outStartTime, outEndTime);

		List<Parkinfo> payInOut = new ArrayList<Parkinfo>();//收支统计
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();
		List<Parkinfo> payStat = new ArrayList<Parkinfo>();
		json = new JSONObject();
		Long[][] data3 = statService.queryByWeeko(payInOut,payStat, paylistInOutByDay, datetime, user.getId());
		if(data3!=null){
			log.info("data3.lenght="+data3.length);
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ) );
			json.put("payStat", JSONArray.fromObject( payStat ) );
			//check 
			if(data3.length!=2){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data3[0].length);
			}
			json.put("data3", data3);
			
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();
		return GUtil.NONE;
	}
	/**
	 * 按月查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByMonth() throws Exception{
		datetime =datetime.substring(0,7);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkMonth(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
		//String datetime = "2014-05";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		List<Parkinfo> payInOut = new ArrayList<Parkinfo>();
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();
		List<Parkinfo> payStat = new ArrayList<Parkinfo>();
		json = new JSONObject();
		Long[][] data2 = statService.queryByMontho(payInOut,payStat,paylistInOutByDay, datetime, user.getId());
		if(data2!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ) );
			json.put("payStat", JSONArray.fromObject( payStat ) );
			//check 
			if(data2.length!=2){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data2[0].length);
			}
			json.put("data2", data2);
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json.toString());
		out.close();
		
		return GUtil.NONE;
	}
	/**
	 * 按年查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByYear() throws Exception{
		datetime =datetime.substring(0,4);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkYear(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy");
		//String datetime = "2014";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		List<Parkinfo> payInOut = new ArrayList<Parkinfo>();
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();
		List<Parkinfo> payStat = new ArrayList<Parkinfo>();
		json = new JSONObject();
		Long[][] data1 = statService.queryByYearo(payInOut,payStat,paylistInOutByDay, datetime, user.getId());
		if(data1!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ));
			json.put("payStat", JSONArray.fromObject( payStat ));
			//check 
			if(data1.length!=2){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data1[0].length);
			}
			json.put("data1", data1);
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json.toString());
		out.close();
		
		return GUtil.NONE;
	}
	/**
	 * @return  绩效统计
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("车场统计");
		log.info("OrderStatistics");
		boolean bPass = true;
		if(stattype==null){
			bPass = false;
			this.setMsg("类型错误"); 
		}
		if(datetime==null){
			bPass = false;
			this.setMsg("时间类型错误"); 
		}
		if(bPass){
			if(stattype.equals("day")){
				return this.queryByDay();
			}else if(stattype.equals("week")){
				return this.queryByWeek();
			}else if(stattype.equals("month")){
				return this.queryByMonth();
			}else if(stattype.equals("year")){
				return this.queryByYear();
			}
			this.setMsg("参数错误  ");
		}
		PrintWriter out = response.getWriter();
		out.print( this.getMsg());
		out.close();

		return GUtil.NONE;
	}
	/**
	 * 导出绩效信息(excel)
	 * 
	 * @return
	 */
	String title;
	public String exportExcel() throws Exception{
		String data = request.getParameter("data");
		
		listPF = (List<ParkStatBean>) session.get("listPF");// for exportExcel
		datetime = (String) session.get("datetime");// for exportExcel
		if(listPF==null){
			this.setMsg("暂无数据");
			return GUtil.SUCCESS;
		}
		title = datetime + "停车场收入额统计";
		String html = "<div>";
		html += "<table style=\"width:800px;\">";
		html += "<tr><td colspan=6 ><center> <h2>"+title+"<h2> </center> </td></tr>";
		//停车场名称、预定量、成交量、应收金额、实收金额
		html += "<tr><td>序号</td><td>停车场名称</td><td>预定总量</td><td>成交总量</td><td>应收总金额</td><td>实收总金额</td></tr>";
		int i=0;
		for(ParkStatBean ps :listPF){
			i++;
			html += "<tr>";
			html += "	<td>"+i+"</td>";
			html += "	<td>"+ps.getParkname()+"</td>";
			html += "	<td>"+ps.getOrdersuncount()+"</td>";
			html += "	<td>"+ps.getOrderokcount()+"</td>";
			html += "	<td>"+ps.getReAmount()+"</td>";
			html += "	<td>"+ps.getAcAmount()+"</td>";
			html += "</tr>";
		}
		html += "</table>";
		html += "</div>";
		request.setAttribute("data", html);
		String url = "/modules/report/excel.jsp?title="+title;
		request.getRequestDispatcher(url).forward(request, response);
		return GUtil.NONE;
	}
	@Override
	public String delete() throws Exception {
		return null;
	}
	@Override
	public String view() throws Exception {
		return null;
	}

	@Override
	public String add() throws Exception {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}
	/**
	 * @return the parkOrder
	 */
	public ParkOrder getParkOrder() {
		return parkOrder;
	}

	/**
	 * @param parkOrder the parkOrder to set
	 */
	public void setParkOrder(ParkOrder parkOrder) {
		this.parkOrder = parkOrder;
	}

	/**
	 * @return the list
	 */
	public List<ParkOrder> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ParkOrder> list) {
		this.list = list;
	}

	/**
	 * @return the queryMyOrder
	 */
	public Boolean getQueryMyOrder() {
		return queryMyOrder;
	}

	/**
	 * @param queryMyOrder the queryMyOrder to set
	 */
	public void setQueryMyOrder(Boolean queryMyOrder) {
		this.queryMyOrder = queryMyOrder;
	}

	public Timestamp getT() {
		return t;
	}

	public void setT(Timestamp t) {
		this.t = t;
	}


	public String getDatetime() {
		return datetime;
	}

	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}


	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	public List<Parkinfo> getListPark() {
		return listPark;
	}

	public void setListPark(List<Parkinfo> listPark) {
		this.listPark = listPark;
	}

	public ParkManager getParkManager() {
		return parkManager;
	}

	public void setParkManager(ParkManager parkManager) {
		this.parkManager = parkManager;
	}

	public Parkinfo getParkInfo() {
		return parkInfo;
	}

	public void setParkInfo(Parkinfo parkInfo) {
		this.parkInfo = parkInfo;
	}

	public List<ParkStatBean> getListPF() {
		return listPF;
	}

	public void setListPF(List<ParkStatBean> listPF) {
		this.listPF = listPF;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public ParkStatService getStatService() {
		return statService;
	}

	public void setStatService(ParkStatService statService) {
		this.statService = statService;
	}

	public Timestamp getTt() {
		return tt;
	}

	public void setTt(Timestamp tt) {
		this.tt = tt;
	}

	public String getStattype() {
		return stattype;
	}

	public void setStattype(String stattype) {
		this.stattype = stattype;
	}
}
